home *** CD-ROM | disk | FTP | other *** search
- Imports System.IO
-
- Public Class FileSystemWatcherForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents fsw As System.IO.FileSystemWatcher
- Friend WithEvents Label1 As System.Windows.Forms.Label
- Friend WithEvents txtPath As System.Windows.Forms.TextBox
- Friend WithEvents Label2 As System.Windows.Forms.Label
- Friend WithEvents chkIncludeSubdirs As System.Windows.Forms.CheckBox
- Friend WithEvents txtLog As System.Windows.Forms.TextBox
- Friend WithEvents chkEnableEvents As System.Windows.Forms.CheckBox
- Friend WithEvents txtFilter As System.Windows.Forms.TextBox
- Friend WithEvents btnWaitForChanged As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.chkEnableEvents = New System.Windows.Forms.CheckBox()
- Me.Label1 = New System.Windows.Forms.Label()
- Me.Label2 = New System.Windows.Forms.Label()
- Me.txtPath = New System.Windows.Forms.TextBox()
- Me.txtFilter = New System.Windows.Forms.TextBox()
- Me.txtLog = New System.Windows.Forms.TextBox()
- Me.btnWaitForChanged = New System.Windows.Forms.Button()
- Me.fsw = New System.IO.FileSystemWatcher()
- Me.chkIncludeSubdirs = New System.Windows.Forms.CheckBox()
- CType(Me.fsw, System.ComponentModel.ISupportInitialize).BeginInit()
- Me.SuspendLayout()
- '
- 'chkEnableEvents
- '
- Me.chkEnableEvents.Location = New System.Drawing.Point(16, 136)
- Me.chkEnableEvents.Name = "chkEnableEvents"
- Me.chkEnableEvents.Size = New System.Drawing.Size(216, 16)
- Me.chkEnableEvents.TabIndex = 6
- Me.chkEnableEvents.Text = "Enable Events"
- '
- 'Label1
- '
- Me.Label1.Location = New System.Drawing.Point(16, 16)
- Me.Label1.Name = "Label1"
- Me.Label1.Size = New System.Drawing.Size(248, 16)
- Me.Label1.TabIndex = 0
- Me.Label1.Text = "Path"
- '
- 'Label2
- '
- Me.Label2.Location = New System.Drawing.Point(16, 72)
- Me.Label2.Name = "Label2"
- Me.Label2.Size = New System.Drawing.Size(248, 16)
- Me.Label2.TabIndex = 0
- Me.Label2.Text = "Filter"
- '
- 'txtPath
- '
- Me.txtPath.Location = New System.Drawing.Point(16, 40)
- Me.txtPath.Name = "txtPath"
- Me.txtPath.Size = New System.Drawing.Size(416, 24)
- Me.txtPath.TabIndex = 1
- Me.txtPath.Text = "c:\"
- '
- 'txtFilter
- '
- Me.txtFilter.Location = New System.Drawing.Point(16, 96)
- Me.txtFilter.Name = "txtFilter"
- Me.txtFilter.Size = New System.Drawing.Size(176, 24)
- Me.txtFilter.TabIndex = 1
- Me.txtFilter.Text = "*.*"
- '
- 'txtLog
- '
- Me.txtLog.Location = New System.Drawing.Point(16, 160)
- Me.txtLog.Multiline = True
- Me.txtLog.Name = "txtLog"
- Me.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtLog.Size = New System.Drawing.Size(424, 104)
- Me.txtLog.TabIndex = 5
- Me.txtLog.Text = ""
- '
- 'btnWaitForChanged
- '
- Me.btnWaitForChanged.Location = New System.Drawing.Point(232, 128)
- Me.btnWaitForChanged.Name = "btnWaitForChanged"
- Me.btnWaitForChanged.Size = New System.Drawing.Size(176, 24)
- Me.btnWaitForChanged.TabIndex = 7
- Me.btnWaitForChanged.Text = "WaitForChanged"
- '
- 'fsw
- '
- Me.fsw.NotifyFilter = ((System.IO.NotifyFilters.FileName Or System.IO.NotifyFilters.DirectoryName) _
- Or System.IO.NotifyFilters.LastWrite)
- Me.fsw.Path = "c:\"
- Me.fsw.SynchronizingObject = Me
- '
- 'chkIncludeSubdirs
- '
- Me.chkIncludeSubdirs.Location = New System.Drawing.Point(224, 96)
- Me.chkIncludeSubdirs.Name = "chkIncludeSubdirs"
- Me.chkIncludeSubdirs.Size = New System.Drawing.Size(176, 16)
- Me.chkIncludeSubdirs.TabIndex = 4
- Me.chkIncludeSubdirs.Text = "Include Subdirectories"
- '
- 'FileSystemWatcherForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(448, 277)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWaitForChanged, Me.chkEnableEvents, Me.txtLog, Me.chkIncludeSubdirs, Me.txtFilter, Me.Label2, Me.txtPath, Me.Label1})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "FileSystemWatcherForm"
- Me.Text = "FileSystemWatcherForm"
- CType(Me.fsw, System.ComponentModel.ISupportInitialize).EndInit()
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- #Const USE_SINGLE_EVENT_HANDLER = False
-
- #If USE_SINGLE_EVENT_HANDLER Then
- ' You can use one event handler for all three events
- Private Sub fsw_All(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Changed, fsw.Created, fsw.Deleted
- Dim changeType As String = [Enum].GetName(GetType(io.WatcherChangeTypes), e.ChangeType)
- LogMessage("File changed: " & e.FullPath & " (" & changeType & ")")
- End Sub
-
- #Else
- ' three separate event handlers
-
- Private Sub fsw_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Changed
- LogMessage("File changed: " & e.FullPath)
- End Sub
-
- Private Sub fsw_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Created
- LogMessage("File created: " & e.FullPath)
- End Sub
-
- Private Sub fsw_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Deleted
- LogMessage("File deleted: " & e.FullPath)
- End Sub
- #End If
-
- ' two more event handlers
-
- Private Sub fsw_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles fsw.Renamed
- LogMessage("File renamed: " & e.OldFullPath & " => " & e.FullPath)
- End Sub
-
- Private Sub fsw_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs) Handles fsw.Error
- LogMessage("FileSystemWatcher error")
- End Sub
-
- ' Add a string to the log
- Sub LogMessage(ByVal msg As String)
- txtLog.AppendText(msg & ControlChars.CrLf)
- End Sub
-
- ' enable/disable events
-
- Private Sub chkEnableEvents_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEnableEvents.CheckedChanged
- Dim f As New System.IO.FileSystemWatcher()
-
- If chkEnableEvents.Checked Then
- fsw.Path = txtPath.Text
- fsw.Filter = txtFilter.Text
- fsw.IncludeSubdirectories = chkIncludeSubdirs.Checked
- fsw.SynchronizingObject = Me
- fsw.NotifyFilter = NotifyFilters.Attributes Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
- fsw.EnableRaisingEvents = True
- Else
- fsw.EnableRaisingEvents = False
- End If
- End Sub
-
- ' demonstrate the WaitForChanged method
-
- Private Sub btnWaitForChanged_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWaitForChanged.Click
- ' Create a *new* FileSystemWatcher component.
- Dim tmpFsw As New FileSystemWatcher(txtPath.Text, txtFilter.Text)
- ' Wait max 10 seconds for an event.
- Dim res As WaitForChangedResult = tmpFsw.WaitForChanged(WatcherChangeTypes.All, 10000)
-
- If res.TimedOut Then
- LogMessage("10 seconds have elapsed without any event")
- Else
- Dim changeType As String = [Enum].GetName(GetType(IO.WatcherChangeTypes), res.ChangeType)
- LogMessage("Event: " & res.Name & " (" & changeType & ")")
- End If
- End Sub
- End Class
-